package com.burgeon.backend.SHOP.rest;
/**
 * 查询库存
 */

import com.burgeon.backend.Rest.base.RESTBase;
import com.burgeon.backend.SHOP.entity.Comment;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;

public class QueryStorage extends RESTBase {

    private String ip;
    private String account;
    private String password;

    public QueryStorage(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     * @return
     */

    public int getData(String gcode) throws Exception {
        String url = "http://"+ip+"/servlets/binserv/Rest";

        String str = RESULT(Data(gcode),url);
        System.err.println(str);
        JSONObject jsonObject = new  JSONArray(str).getJSONObject(0);
        String code = jsonObject.getString("code");
        String message = jsonObject.getString("message");

        if (!"0".equals(code)){
           throw new Exception(message);
        }
        int count = 0;
        JSONArray jsonArray = jsonObject.getJSONArray("rows");
        for (int i=0;i<jsonArray.length();i++){
          String data =   jsonArray.getJSONArray(i).getString(0);
          count += Integer.parseInt(data);
        }

        return count;
    }

    private MultiValueMap Data(String code) {
        HashMap params = new HashMap();
        ArrayList columns = new ArrayList();//查询字段

        columns.add("QTYCAN");

        params.put("columns", columns);
        params.put("table", "FA_STORAGEYC_SKU");//表名

        params.put("count","true");

        HashMap paramss = new HashMap();
        String ac = "'"+ code+"'";
        paramss.put("column", "'M_PRODUCTALIAS_ID;NO'");
        paramss.put("condition", ac);
        params.put("params", paramss);

        return DATA("Query", params,account,password);
    }

    public static void main(String[] args) {
        QueryStorage getRetailByVip = new QueryStorage("59.110.28.94:90", "nea@burgeon.com.cn", "burgeon55321");
        try {
            System.err.println(getRetailByVip.getData("BDN2KD562"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
